package com.zx.sdk.league.member;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jd.ad.sdk.fdt.utils.ScreenUtils;
import com.zx.sdk.R;
import com.zx.sdk.ZxSDK;
import com.zx.sdk.listener.BaseZxBannerListener;
import com.zx.sdk.listener.BaseZxRewardListener;
import com.zx.sdk.listener.BaseZxSplashListener;
import com.zx.sdk.model.AdInfo;
import com.zx.sdk.model.SuspensionInfo;

public class Ta extends BaseLeagueMember<Object, Object, Object, Object, SuspensionInfo> {
    @Override
    public String getName() {
        return "ta";
    }

    @Override
    protected void onInit(Context context, String appId, boolean initialized) {

    }

    @Override
    protected void onLoadSplash(AdInfo adInfo, ViewGroup container, String mode, String type, BaseZxSplashListener listener) {

    }

    @Override
    public boolean onShowSplash(AdInfo adInfo, ViewGroup container, @Nullable Object splash, BaseZxSplashListener listener) {
        return false;
    }

    @Override
    public void onLoadReward(Activity context, AdInfo adInfo, String mode, String type, BaseZxRewardListener listener) {

    }

    @Override
    public boolean onShowReward(Activity context, AdInfo adInfo, @Nullable Object reward, BaseZxRewardListener listener) {
        return false;
    }

    @Override
    public void onLoadInterstitial(Activity context, AdInfo adInfo, String mode, String type, BaseZxRewardListener listener) {

    }

    @Override
    public boolean onShowInterstitial(Activity context, AdInfo adInfo, @Nullable Object interstitial, BaseZxRewardListener listener) {
        return false;
    }

    @Override
    protected void onLoadSuspension(Activity context, ViewGroup container, AdInfo adInfo, String mode, String type, BaseZxBannerListener listener) {
        super.onLoadSuspension(context, container, adInfo, mode, type, listener);
        try {
            SuspensionInfo info = new Gson().fromJson(adInfo.getExtend(), SuspensionInfo.class);
            if (info != null) {
                switch (mode) {
                    case ZxSDK.FETCH_ONLY:
                        saveSuspension(adInfo.getMapPid(), info);
                        break;
                    case ZxSDK.FETCH_AND_SHOW:
                        onShowSuspension(context, adInfo, container, info, listener);
                        break;
                }
            }
        } catch (Exception ignored) {

        }
    }

    @Override
    protected boolean onShowSuspension(Activity context, AdInfo adInfo, ViewGroup container, SuspensionInfo suspension, BaseZxBannerListener listener) {
        if (suspension == null) return false;
        container.removeAllViews();
        ImageView imageView = new ImageView(context);
        imageView.setOnClickListener(v -> listener.onAdClick(suspension, Ta.this, adInfo));
        container.addView(imageView, (int) ScreenUtils.dip2px(context, 66F), (int) ScreenUtils.dip2px(context, 66F));
        Glide.with(context).load(suspension.iconUrl).into(imageView);
        ImageView closeBtn = new ImageView(context);
        closeBtn.setImageResource(R.drawable.adsuyi_icon_close);
        closeBtn.setOnClickListener(v -> {
            container.removeAllViews();
            listener.onAdClose(suspension, Ta.this, adInfo);
        });
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams((int) ScreenUtils.dip2px(context, 20F), (int) ScreenUtils.dip2px(context, 20F));
        layoutParams.gravity = Gravity.END;
        container.addView(closeBtn, layoutParams);
        listener.onAdShow(suspension, Ta.this, adInfo);
        return true;
    }
}
